import { Asset, AssetManager, assetManager, SpriteFrame } from "cc";

export class ResMgr {
    public static get Assets() {
        return assetManager.assets;
    }

    public static getSpriteFrame<T extends Asset>(Bundle:string,resName:string,relative_path:string=""):T{
        return this.getRes<T>(Bundle,resName+"/spriteFrame",relative_path);
    }

    public static getRes<T extends Asset>(Bundle:string,resName:string,relative_path:string=""):T{
        let bundle:AssetManager.Bundle = assetManager.getBundle(Bundle);
        if(!bundle){
            console.error("bundle is null");
            return null;
        }
        let path:string = relative_path + resName;
        const res:T = bundle.get(path);
        if(!res){
            console.error("res is null");
            return null;
        }
        return res;
    }

    public static getSpriteRes(resName:string,relative_path:string=""):Promise<SpriteFrame>{
        return new Promise<SpriteFrame>((resolve,reject)=>{
            assetManager.loadBundle("bundle",(err,bundle)=>{
                if(err){
                    console.log("err",err);
                    reject(err);
                }
    
                let path:string = relative_path + resName;
                
                bundle.load(path, SpriteFrame, (err, spr) => {
                    if(err){
                        console.log("err",err);
                        reject(err);
                    }
                    resolve(spr);
                })
            })
        })
        
    }
}